package org.jeecg.modules.device.com.pudong.service.impl;

import org.jeecg.modules.device.com.pudong.entity.TimeConsistencyCategory;
import org.jeecg.modules.device.com.pudong.entity.TimeConsistencyParamMap;
import org.jeecg.modules.device.com.pudong.mapper.TimeConsistencyParamMapMapper;
import org.jeecg.modules.device.com.pudong.mapper.TimeConsistencyCategoryMapper;
import org.jeecg.modules.device.com.pudong.service.ITimeConsistencyCategoryService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: time_consistency_category
 * @Author: jeecg-boot
 * @Date:   2025-10-30
 * @Version: V1.0
 */
@Service
public class TimeConsistencyCategoryServiceImpl extends ServiceImpl<TimeConsistencyCategoryMapper, TimeConsistencyCategory> implements ITimeConsistencyCategoryService {

	@Autowired
	private TimeConsistencyCategoryMapper timeConsistencyCategoryMapper;
	@Autowired
	private TimeConsistencyParamMapMapper timeConsistencyParamMapMapper;
	
	@Override
	@Transactional
	public void saveMain(TimeConsistencyCategory timeConsistencyCategory, List<TimeConsistencyParamMap> timeConsistencyParamMapList) {
		timeConsistencyCategoryMapper.insert(timeConsistencyCategory);
		if(timeConsistencyParamMapList!=null && timeConsistencyParamMapList.size()>0) {
			for(TimeConsistencyParamMap entity:timeConsistencyParamMapList) {
				//外键设置
				entity.setCategoryCode(timeConsistencyCategory.getCategoryCode());
				timeConsistencyParamMapMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(TimeConsistencyCategory timeConsistencyCategory,List<TimeConsistencyParamMap> timeConsistencyParamMapList) {
		timeConsistencyCategoryMapper.updateById(timeConsistencyCategory);
		
		//1.先删除子表数据
		timeConsistencyParamMapMapper.deleteByMainId(String.valueOf(timeConsistencyCategory.getId()));
		
		//2.子表数据重新插入
		if(timeConsistencyParamMapList!=null && timeConsistencyParamMapList.size()>0) {
			for(TimeConsistencyParamMap entity:timeConsistencyParamMapList) {
				//外键设置
				entity.setCategoryCode(timeConsistencyCategory.getCategoryCode());
				timeConsistencyParamMapMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		timeConsistencyParamMapMapper.deleteByMainId(id);
		timeConsistencyCategoryMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			timeConsistencyParamMapMapper.deleteByMainId(id.toString());
			timeConsistencyCategoryMapper.deleteById(id);
		}
	}
	
}
